﻿function BitcoinViewModel(model) {
    selfBitcoin = this;
    ko.mapping.fromJS(model, {}, selfBitcoin);

    selfBitcoin.Refresh = function () {
        $.ajax({
            url: "Bitcoin/GetState",
            type: 'GET',
            data: null,
            contentType: 'application/json',
            success: function (result) {
                if (!result.Success) {
                    if (result.Data) {
                        selfBitcoin.WeightledPrices(result.Data.WeightledPrices);
                        selfBitcoin.LastUpdate(result.Data.LastUpdate);
                    }
                }
                else
                    if (result.MustRedirect)
                        window.location.href = result.RedirectURL;
            }
        });
    };

    //Ensure that the request is after server side refresh
    setTimeout(selfBitcoin.Refresh, selfBitcoin.RefreshTimeout());
}
